package com.yf.stat.modules.course.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yf.ability.excel.ExportExcel;
import com.yf.base.api.api.ApiRest;
import com.yf.base.api.api.controller.BaseController;
import com.yf.base.api.api.dto.BaseIdReqDTO;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.stat.modules.course.dto.StatCourseTotalDTO;
import com.yf.stat.modules.course.dto.request.StatCourseFileReqDTO;
import com.yf.stat.modules.course.dto.request.StatCourseLearnReqDTO;
import com.yf.stat.modules.course.dto.response.StatCourseDashDTO;
import com.yf.stat.modules.course.dto.response.StatCourseFileDTO;
import com.yf.stat.modules.course.dto.response.StatCourseLearnDTO;
import com.yf.stat.modules.course.mapper.StatCourseMapper;
import com.yf.stat.modules.course.service.StatCourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
* <p>
* 课程统计控制器
* </p>
*
* @author 聪明笨狗
* @since 2020-09-11 11:10
*/
@Log4j2
@Api(tags={"课程总体统计"})
@RestController
@RequestMapping("/api/stat/course")
public class StatCourseController extends BaseController {

    @Autowired
    private StatCourseService baseService;

    @Autowired
    private ExportExcel exportExcel;

    @Autowired
    private StatCourseMapper statCourseMapper;

    /**
     * 查找课程总览信息
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "查找课程总览信息")
    @PostMapping("/overview")
    public ApiRest<StatCourseTotalDTO> overview(@RequestBody BaseIdReqDTO reqDTO) {
        StatCourseTotalDTO dto = baseService.findOverview(reqDTO.getId());
        return super.success(dto);
    }

    /**
     * 课件统计总览
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "课件统计总览")
    @PostMapping("/file-stat-paging")
    public ApiRest<IPage<StatCourseFileDTO>> fileStatPaging(@RequestBody PagingReqDTO<StatCourseFileReqDTO> reqDTO) {
        IPage<StatCourseFileDTO> dto = baseService.fileStatPaging(reqDTO);
        return super.success(dto);
    }


    /**
     * 课件统计总览
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "课件统计总览")
    @PostMapping("/file-stat-export")
    public ApiRest<IPage<StatCourseFileDTO>> fileStatExport(HttpServletResponse response, @RequestBody StatCourseFileReqDTO reqDTO) {

        PagingReqDTO<StatCourseFileReqDTO> query = new PagingReqDTO<>();
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        query.setParams(reqDTO);

        IPage<StatCourseFileDTO> dto = baseService.fileStatPaging(query);

        List<StatCourseFileDTO> list = dto.getRecords();

        // 执行导出
        exportExcel.export(response, StatCourseFileDTO.class, "课件统计", list);


        return super.success(dto);
    }

    /**
     * 用户学习统计总览
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "用户学习统计总览")
    @PostMapping("/user-stat-paging")
    public ApiRest<IPage<StatCourseLearnDTO>> userStatPaging(@RequestBody PagingReqDTO<StatCourseLearnReqDTO> reqDTO) {
        IPage<StatCourseLearnDTO> dto = baseService.userStatPaging(reqDTO);
        return super.success(dto);
    }


    /**
     * 课件统计总览
     * @param reqDTO
     * @return
     */
    @ApiOperation(value = "用户学习统计总览导出")
    @PostMapping("/user-stat-export")
    public ApiRest<IPage<StatCourseLearnDTO>> userStatExport(HttpServletResponse response, @RequestBody StatCourseLearnReqDTO reqDTO) {

        PagingReqDTO<StatCourseLearnReqDTO> query = new PagingReqDTO<>();
        query.setCurrent(1);
        query.setSize(Integer.MAX_VALUE);
        query.setParams(reqDTO);

        IPage<StatCourseLearnDTO> dto = baseService.userStatPaging(query);

        List<StatCourseLearnDTO> list = dto.getRecords();

        // 导出文件
        exportExcel.export(response, StatCourseLearnDTO.class, "学习情况统计", list);

        return super.success(dto);
    }


    /**
     * 控制台学习列表
     * @return
     */
    @ApiOperation(value = "控制台学习列表")
    @PostMapping("/dash-course-list")
    public ApiRest<List<StatCourseDashDTO>> dashCourseList() {
        List<StatCourseDashDTO> list = statCourseMapper.dashCourseList();
        return super.success(list);
    }

}
